package com.example.iotest.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class ThreadPoolServer {
    static byte[] buf=new byte[64];
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(8081));
        log.info("服务器启动成功");
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        while (true) {
            Socket accept = serverSocket.accept();
            Callable<Integer> callable = () -> {
                InetAddress inetAddress = accept.getInetAddress();
                System.out.println(inetAddress + "连接");
                OutputStream outputStream = accept.getOutputStream();
                File file = new File("K:\\电影\\未麻的部屋BD日语中字[电影天堂www.dy2018.com].mp4");
                String fileLength = file.length()+"";
                System.out.println(fileLength);
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int length = 0;
                byte[] bytes = fileLength.getBytes();
                for (int i = 0; i < bytes.length; i++) {
                    buf[i]=bytes[i];
                }
                outputStream.write(buf);
                while ((length = fileInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, length);
                }
                outputStream.close();
                fileInputStream.close();
                return 1;
            };
            executorService.submit(callable);
        }
    }
}